<?php
    $this->headTitle($this->enterprise->company_name_vn.': '.$this->translator('front_end_product_list'));

    $this->headLink()
        ->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.core.css')
        ->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.resizable.css')
        ->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.dialog.css')
        ->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.theme.css');

    $this->headScript()
        ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.core.js')
        ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.draggable.js')
        ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.droppable.js')
        ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.resizable.js')
        ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.dialog.js')
?>
<style>
    .col_50l{
        float:left;
        width:49%;
    }
    .col_50r{
        float:right;
        width:49%;
    }
    .subrow{
        padding-bottom:10px;
        display:table;
        width:100%;
    }
    .caption{
    	font-weight: bold;
    	color: #ffffff;
		background: #BBCD00;
		border: #BBCD00 1px solid;
		border-right: 0;
    }
    .border_style{
    	border:#EEEEEE 1px solid;
    }
    .div_1{
    	width: 32%; float: left;
    	padding-left: 7px;
    }
    .div_2{
    	display: table ;width: 92%; 
    	padding-left: 40px;
    	padding-top:3px;
    }
</style>
<?php
    $skin_url = $this->APP_STATIC_SERVER . '/skins/'. $this->APP_TEMPLATE . '/'. $this->APP_SKIN;
?>
<div class="grid_12">
    <div class="grid_8 alpha">
        <?php if(!$this->found):?>
        <?php echo $this->translator('not_found_database');?>
        <?php else:?>
        <!--sub menu -->
        <?php echo $this->widget('bizprofile','menu',array()); ?>
        <!--end sub menu -->
        <?php if($this->objCategory):?>
        <div class="subcolumns pb10">
            <?php
                $arrParamProduct = array(
                    'enterprise_id' => $this->enterprise->id,
                    'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true)
                );
            ?>
            <a href="<?php echo $this->url($arrParamProduct, 'shop_product_products');?>"><?php echo $this->translator('product_list_breakcrumb');?></a> »
            <?php echo ($this->objCategory->category_content_name)?$this->objCategory->category_content_name:$this->objCategory->category_admin_name;?>
        </div>
        <?php endif;?>
        <div class="subcolumns pb10 border_style" style="margin-bottom: 5px;">
            
			<div class="caption icon_title">
				<span style="padding: 7px;"><?php echo $this->translator('category_title');?></span>
			</div>
			<div class="div_2">
            <?php $ii = 0;foreach($this->categories as $category):?>
                <?php
                    $arrParamCat = array(
                        'enterprise_id' => $this->enterprise->id,
                        'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true),
                        'cat' => $category->category_id,
                        'slug' => ($category->slug)?$category->slug:Tomato_Utility_String::removeSign($category->category_admin_name, '-', true),
                    );
                ?>
                
                    <div class="subcolumns_oldgecko div_1">
                        <a href="<?php echo $this->url($arrParamCat,'shop_product_products_cat');?>">
                           <img src="<?php echo $this->APP_STATIC_SERVER;?>/skins/bizprofile/default/images/icon.jpg" style="padding-bottom:1px; padding-right: 3px;">
                        	<?php echo ($category->category_content_name)?$category->category_content_name:$category->category_admin_name;?> (<?php echo $this->arrCounted[$category->category_id];?>)
                        </a>
                    </div>
				<?php if($ii%3 == 2){?>                
				</div><div class="div_2">
				<?php }?>
            <?php $ii++;endforeach;?>
            </div>
        </div>
        <div class="subcolumns pb10">
            <?php if($this->count > 0):?>
            <?php $i=0; foreach ($this->products as $product):?>
            <?php            
            	/**
                 * Get quantity product existed in cart
                 */
                $qtyItems = $this->arrQtyItems[$product->product_id];
                /**
                * Category follow product
                **/
                $objCategory = $this->arrPC[$product->product_id];

                /**
                 * Param url detail
                **/
                $arrRouteDetail = array(
                    'enterprise_id' => $this->enterprise->id,
                    'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true),
                    'cat' => $objCategory->category_id,
                    'slug' => ($objCategory->slug)?$objCategory->slug:Tomato_Utility_String::removeSign($objCategory->category_admin_name, '-', true),
                    'product_id' => $product->product_id,
                    'pro_slug' => ($product->slug)?$product->slug:Tomato_Utility_String::removeSign($product->product_admin_name, '-', true),
                );

                $quantity = null;
                $coreQty = null;
                if('non_deliver_able' == $product->product_status_of_delivery){
                    $quantity = "<span style='color:red;'>".$this->translator("product_status_of_delivery_non_deliverable")."</span>";
                    $coreQty = $product->product_status_of_delivery;
                }
                elseif('please_contact' == $product->product_status_of_delivery){
                    $quantity = $this->translator("product_status_of_delivery_".$product->product_status_of_delivery);
                    $coreQty = $product->product_status_of_delivery;
                }
                else{
					//$quantity = ($product->product_quantity)?$product->product_quantity-$qtyItems:"<span style='color:red;'>".$this->translator("product_status_of_delivery_non_deliverable")."</span>";
                    $quantity = ($product->product_quantity)?$product->product_quantity:"<span style='color:red;'>".$this->translator("product_status_of_delivery_non_deliverable")."</span>";
                    $coreQty = $product->product_quantity_mode;
                }
				
				$p_name = ($product->product_content_name)?$product->product_content_name:$product->product_admin_name;       
				
            ?>
            <?php if($i%2==0):?>
            <div class="subrow">
            <?php endif;?>
                <div class="<?php if($i%2==0):?>col_50l<?php else:?>col_50r<?php endif;?>">
                    <div class="subcolumns_oldgecko">
                        <a href="<?php echo $this->url($arrRouteDetail,'shop_product_detail');?>"><span class="title"><?php echo $p_name;?></span></a>
                    </div>
                    <div class="subcolumns_oldgecko">
                        <div class="geckol" style="width:42%;">
							<?php if($product->getImage()):?>
								<a href="<?php echo $this->url($arrRouteDetail,'shop_product_detail');?>">
									<img width="130" src="<?php echo $product->getImage('_small', 200);?>" class="item_img" alt="<?php echo $p_name;?>">
								</a>
							<?php endif;?>						                           
                        </div>						
                        <div class="geckor" style="width: 56%;">
                            <div style="text-align:justify">
                                <?php echo strip_tags(Tomato_Helper_Helper::substring(strip_tags($product->product_content_description), 70));?>
                            </div>
                            <div>
                                <?php echo $this->translator('front_product_price');?>:
                                <strong><?php echo $product->product_price;?> <?php echo $product->product_price_currency;?></strong>
                            </div>
                            
                            <!--Show gia van chuyen va nguon-->
                            <div style="width:100%; display:table;">
								<?php if($product->product_shipping_price_1 > 0):?>
	                            <div style="width:50%; float:left;">
	                                <i><?php echo $this->translator('front_product_shipping_price')?></i><br />
	                                <strong><?php echo $product->product_shipping_price_1;?> <?php echo $product->product_price_currency;?></strong>
	                            </div>
								<?php endif;?>
								
	                            <?php if($coreQty != 'infinity'):?>								
	                            <div style="width:50%; float:left;">
	                                <?php echo $this->translator('front_product_source')?></i><br />
	                                <strong id="qtyText_<?php echo $product->product_id; ?>"><?php echo $quantity;?></strong>
	                                <input type="hidden" value="<?php echo $product->product_quantity-$qtyItems;?>" id="oldQty_<?php echo $product->product_id;?>" />                                
	                            </div>								
	                            <?php endif;?>
                            </div>
                            
                            <!--form add to cart-->
                            <?php if($coreQty != "non_deliver_able" && $coreQty != "please_contact" && ($coreQty == 'infinity' || $product->product_quantity > 0)):?>
                            <div style="width:100%">
								<?php 
									$conn = Tomato_Db_Connection::factory()->getMasterConnection();
									$productAttributeDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getProductAttributeDao();
									$productAttributeDao->setDbConnection($conn);
									$productAttribute = $productAttributeDao->getAttributeTypeIdByProductId($product->product_id);									
								?>
                                <form action="<?php echo $this->url(array('product_id' => $product->product_id), 'shoppingcart_shoppingcart_add');?>" method="POST" id="addCartFrom<?php echo $product->product_id;?>" onkeypress="return event.keyCode!=13">
                                    <?php echo $this->translator('front_product_quantity');?>: <input type="text" name="quantity" rel="<?php echo $product->product_id;?>" class="qtyItems" id="quantity_<?php echo $product->product_id;?>" style="width:50px" />
                                    <a rel="<?php echo $product->product_id;?>" title="<?php echo $this->translator('product_add_to_cart_btn')?>" href="javascript:void(0)" class="add_btn <?php if(count($productAttribute)>0) echo 'has_attr';?>">
                                        <img style="cursor:pointer;" src="<?php echo $skin_url;?>/html/images/icon-cart.png" />
                                    </a>
                                    <div style="color:red" id="messageError_<?php echo $product->product_id;?>"></div>
                                </form>
                            </div>
                            <?php endif;?>
                            <input type="hidden" value="<?php echo $product->product_quantity_mode; ?>" id="qtyMode_<?php echo $product->product_id;?>" />
                        </div>
                    </div>
                </div>
            <?php $i++; if($i%2==0):?>
            </div>
            <?php endif;?>
            <?php endforeach;?>
            <?php if($i%2!=0):?>
            </div>
            <?php endif;?>

            <?php else:?>
            <div id="latest_product"><?php echo $this->translator('product_not_found_products')?></div>
            <?php endif;?>
        </div>

        <div id="messageDialog"></div>

        <script type="text/javascript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.form/jquery.form.js"></script>
        <script type="text/javascript" charset="utf-8">
        $(document).ready(function() {
            /**
            * Ajax submit form
            */
            $('.add_btn').click(function(){		
				var has_attr = $(this).hasClass('has_attr');
                var self 	 = this;
                var id 		 = $(self).attr('rel');
            	var quantity = 'quantity_'+id;            	
            	var oldQty 	 = 'oldQty_'+id;            	
            	var qtyMode	 = 'qtyMode_'+id;            	
                
            	var oldQtyVal 	= $('#'+oldQty).val();
				var quantityVal = $('#'+quantity).val();
				var qtyModeVal  = $('#'+qtyMode).val();
				if(quantityVal <= 0){
					quantityVal = 1;
				}
                
				if(IsNumeric(quantityVal)){					
					//if(quantityVal <= parseInt(oldQtyVal) || qtyModeVal == 'infinity'){
		                $(self).html('').addClass('t_a_ui_helper_loading');		                
		                var options = {
		                    target: '#outPut', // target element(s) to be updated with server response
		                    beforeSubmit:  showRequest,
		                    type: 'POST',  // pre-submit callback,
		                    dataType: 'html',
		                    success: function(responseText, statusText){
		                        $(self).removeClass('t_a_ui_helper_loading')
		                        .append($('<img style="cursor:pointer;" src="<?php echo $skin_url;?>/html/images/icon-cart.png" />').html(''));
		                        
		                        /**
		                        * Update quantity
		                        */		                        
		                        /*qtyText = $('#qtyText_'+id).text();
		                        qtyText1 = qtyText-quantityVal;		                        
		                        $('#qtyText_'+id).text(qtyText1);
		                        $('#oldQty_'+id).val(qtyText1);*/
		                        
		                        /**
		                        * Update num cart item
		                        */
		                        num_cartItem = $('#num_cartItem').text();		                        
		                        num_cartItem = parseInt(num_cartItem)+1;
		                        $('#num_cartItem').text(num_cartItem);
		
		                        if('' != responseText){
		                            $('#messageDialog').html(responseText)
		                            .dialog({
		                                title: '<?php echo addslashes($this->translator('common_dialog_message')); ?>',
		                                dialogClass: 't_a_ui_dialog',
		                                modal: true,
		                                show: { effect: 'fade' },
		                                open: function(event, ui) {
		                                    /*setTimeout(function() {
		                                        $('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
		                                    }, 300000);*/
		                                },
		                                close: function(event, ui){
		                                    $('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
		                                }
		                            });
		                        }
								if(has_attr==false){
		                            $('#messageDialog').html('<?php echo $this->translator('shoppingcart_message_success');?>')
		                            .dialog({
		                                title: '<?php echo addslashes($this->translator('common_dialog_message')); ?>',
		                                dialogClass: 't_a_ui_dialog',
		                                modal: true,
		                                show: { effect: 'fade' },
		                                open: function(event, ui) {
											
		                                },
		                                close: function(event, ui){
		                                    $('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
		                                }
		                            });
								}
		                    }
		                };
		                $("#addCartFrom"+id).ajaxSubmit(options);
		            /*}
	                else{
						$('#'+quantity).focus();					
					}*/
				}
				else{				
					$('#'+quantity).focus();
				}
            });
            
            
            $('.qtyItems').keyup(function() {
            	/**
            	* Lấy ra tất cả tên của input tương ứng cho từng sản phẩm
            	*/
            	var self 		 = this;
            	var rel 		 = $(self).attr('rel');
            	var oldQty 		 = 'oldQty_'+rel;
            	var quantity 	 = 'quantity_'+rel;
            	var messageError = 'messageError_'+rel;
            	var qtyMode 	 = 'qtyMode_'+rel;            	
            	
            	/**
            	* Lấy giá trị
            	*/            	
				var oldQtyVal 	= $('#'+oldQty).val();
				var quantityVal = $('#'+quantity).val();
				var qtyModeVal  = $('#'+qtyMode).val();
				
				if(IsNumeric(quantityVal)){
					$('#'+quantity).focus();
					$('#'+messageError).text("");
					
					if(quantityVal <= parseInt(oldQtyVal) || qtyModeVal == 'infinity'){					
						$('#'+quantity).focus();
						$('#'+messageError).text("");
					}
					/*else{
						$('#'+quantity).focus();
						$('#'+messageError).text("<?php echo $this->translator('product_detail_over_qty_message');?>");
					}*/
				}else{
					$('#'+quantity).focus();
					$('#'+messageError).text("<?php echo $this->translator('product_detail_no_num_message');?>");				
				}
			});
            
        })
        function showRequest(formData, jqForm, options) {
            var queryString = $.param(formData);
            return true;
        }
        function IsNumeric(sText)
		{
		   var ValidChars = "0123456789.";
		   var IsNumber=true;
		   var Char;	 
		   for (i = 0; i < sText.length && IsNumber == true; i++) 
			{ 
			  Char = sText.charAt(i); 
			  if (ValidChars.indexOf(Char) == -1) 
				{
					IsNumber = false;
				}
			}
		   return IsNumber;	   
		}
        </script>
        <?php endif;?>
    </div>
    <div class="grid_4 omega">
        <?php echo $this->widget('bizprofile','map',array()); ?>
        <?php echo $this->widget('bizprofile','help',array()); ?>
    </div>
</div>
<div class="grid_12">
    <?php echo $this->widget('bizprofile','contact',array()); ?>
</div>